package com.yyq.adms.usermanage.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.usermanage.bpo.SysDeptsBpo;
import com.yyq.adms.usermanage.bpo.SysUsersBpo;
import com.yyq.adms.usermanage.pojo.SysDepts;
import com.yyq.adms.usermanage.pojo.SysUsers;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;

@Controller("sysUserAction")
@Scope("prototype")
public class SysUserAction extends BaseAction
{
	private static final long serialVersionUID = 970236665615134603L;

	SysUsers po = new SysUsers();
	private SysUsersBpo bpo;
	private SysDeptsBpo dBpo;
	
	
	List userRoles;

	/**
	 * Action操作入口
	 */
	public String execute()throws Exception
	{
		super.setBpo(bpo);
		super.setObj(po);
		String ss = "";
		ss = super.execute();
		return ss;
	}

	public String data() {
		SysDepts vo = (SysDepts)dBpo.findByPk(po.getDeptId());
		String where = " o where ";
		if(vo.getDeptType().equals("1")){
			List list = dBpo.search("from "+SysDepts.class.getName()+" o where o.deptType='0' and o.parentId="+po.getDeptId());
			where += " o.deptId="+po.getDeptId();
			for(int i=0; i<list.size(); ++i){
				where += " or o.deptId="+((SysDepts)list.get(i)).getId();
			}

		}else{
			where += " o.deptId="+po.getDeptId();
		}
		String hql = "from "+SysUsers.class.getName()+where;
		this.setFormData(bpo.searchPage(hql, this.getPageForm().getPageNo(), this.getPageForm().getPageSize()));
		return ActionType.ACTION_DATA_LIST;
	}

//	public String add() {
//		SysDepts vo = (SysDepts)BpoFactory.getSysDeptBpo(SysDepts.class).findByPk(new Integer(po.getDeptId()+""));
//		po.setDeptLevel(vo.getDeptLevel());
//		return super.add();
//	}
	
	public String add()
	{
		SysDepts vo = (SysDepts)dBpo.findByPk(po.getDeptId());
		po.setDeptLevel(vo.getDeptLevel());
		//String passwd=getInitPassword();
		String passwd="123456";
//		po.setPassword(EncryptUtil.encrypt(passwd));
    	po.setPassword(passwd);
		java.io.Serializable id =null;

		try {
		System.out.println(po.getUserCname());
			String cname = new String(po.getUserCname().getBytes("gbk"),"utf-8");
		System.out.println(cname);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		boolean flag = bpo.exist("from "+po.getClass().getName()+" where userName='"+po.getUserName()+"'");
		if(flag){
			this.setMsg("{processId:\"-1\",msg:\"登录名已存在!\"}");
		}else{
			if(po!=null){
				id = bpo.save(po);
				
			}
			if(id!=null){
				this.setMsg("{processId:\"1\",msg:\"保存成功!\"}");
			}
			else{
				this.setMsg("{processId:\"-1\",msg:\"添加失败!\"}");
			}
		}
		return ActionType.ACTION_MSG;
	}
	

	public String goUpdate()
	{
		String[] types = (String[]) this.getFormValue("type");
		//判断是否带有formMap.type值，若有，则为修改密码请求
		if(types!=null&&types[0].equals("modPW")){
			LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
			po = (SysUsers) bpo.findByPk(user.getUserId());
			return "modPW";
		}

		po = (SysUsers) bpo.findByPk(po.getId());
		SysDepts vo = (SysDepts)dBpo.findByPk(po.getDeptId());
		String hql = "from "+SysDepts.class.getName()+" o where o.deptLevel like '";
		if(vo.getDeptType().equals("0")){
			hql += vo.getDeptLevel().substring(0, vo.getDeptLevel().length()-2)+"%'";
		}else{
			hql += vo.getDeptLevel()+"%'";
		}
//		String hql = "from "+SysDepts.class.getName();  //查询所有部门
		this.setFormValue("depts", bpo.search(hql));
		return super.goUpdate();
	}

	public String update() {
		if(po.getPassword()==null||"".equals(po.getPassword().trim())){
//			System.out.println("********************************");
//			System.out.println(po.getUserId());
//			System.out.println(po.getUserCname());
//			System.out.println(po.getDeptId()+":"+po.getDeptLevel()+":"+po.getDeptName());
//			System.out.println(po.getFullName());
//			System.out.println(po.getTelephone());
//			System.out.println(po.getUserEmail());
//			System.out.println(po.getSts());
//			System.out.println(po.getRemark());
			String hql="update "+SysUsers.class.getName()+" set userCname='"+po.getUserCname()+"',deptId='" 
					+po.getDeptId()+"',fullName='"+po.getFullName()+"',telephone='"+po.getTelephone()
					+"',userEmail='"+po.getUserEmail()+"',sts='"+po.getSts()+"',remark='"+po.getRemark()+"',"
					+" where userId='"+po.getId()+"'";
			boolean flag=bpo.update(hql);
			if(flag){
				this.setMsg("{processId:\"1\",msg:\"更新成功!\"}");
			}else{
				this.setMsg("{processId:\"-1\",msg:\"更新失败!\"}");
			}
			return ActionType.ACTION_MSG;
		}
		SysDepts vo = (SysDepts)dBpo.findByPk(new Integer(po.getDeptId()+""));
		po.setDeptLevel(vo.getDeptLevel());
		//检查次密码是否用过？
		return super.update();
	}

	public SysUsers getPo()
	{
		return po;
	}

	public void setPo(SysUsers po)
	{
		this.po = po;
	}

	public List getUserRoles()
	{
		return userRoles;
	}

	public void setUserRoles(List userRoles)
	{
		this.userRoles = userRoles;
	}
	
	//获得初始密码
	/*public String getInitPassword() {
		int no=Integer.parseInt(SysParametersUtil.getInstance().getValue("PASSWORD_MIN_DIGITS"));
		String initPassword="";
		String regex="^\\p{Lower}+\\d+\\p{Upper}+\\w*$";
		while (true) {
			initPassword = RandomUtil.getRandomNormalString(no);
			if(initPassword.matches(regex))
				break;
		}
		return initPassword;
	}*/

	@Resource(name="sysUsersBpo")
	public void setBpo(SysUsersBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	@Resource(name="sysDeptsBpo")
	public void setDBpo(SysDeptsBpo bpo) {
		bpo.setCls(SysDepts.class);
		dBpo = bpo;
	}
}
